package ljl.alg.wangzheng_camp.round1.tree;

import commons.TreeNode;

public class _572_is_subtree {
    
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if (root == null) return subRoot == null;
        return has(root, subRoot) || isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
    }
    
    boolean has(TreeNode p, TreeNode s) {
        if (s == null || p == null) return p == s;
        return s.val == p.val && has(p.left, s.left) && has(p.right, s.right);
    }
}
